package com.yoomiito.app.widget.dialog;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.listener.OnViewClickListener;
import com.yoomiito.app.model.home.FreeGoodsBuyInfo;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.RoundImageView;

public class FreeGoodsCouponDialog extends BaseDialog implements View.OnClickListener {
    private OnViewClickListener mOnViewClickListener;
    private FreeGoodsBuyInfo mFreeGoodsBuyInfo;

    public FreeGoodsCouponDialog(@NonNull Context context) {
        this(context,0);
    }

    public FreeGoodsCouponDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    protected FreeGoodsCouponDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public View setView() {
        setmHeightPercent(1);
        setWidthPercent(1);
        setCanceledOnTouchOutside(true);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_free_goods_coupon,null,false);
        view.findViewById(R.id.use).setOnClickListener(this);
        view.findViewById(R.id.dismiss).setOnClickListener(this);
        if (mFreeGoodsBuyInfo!=null){
            RoundImageView roundImageView = view.findViewById(R.id.goods_pic);
            ImageLoader.getInstance().into(mContext,mFreeGoodsBuyInfo.getImage_url(),roundImageView);
            TextView goodsName = view.findViewById(R.id.goods_name);
            goodsName.setText(SpannableUtils.getIconAndText(mFreeGoodsBuyInfo.getTitle(), ResUtil.getDrawable(mFreeGoodsBuyInfo.getSource().equals("1")?R.mipmap.tmall:R.mipmap.tb)));
            TextView oldPriceTv = view.findViewById(R.id.goods_old_price);
            oldPriceTv.setText(SpannableUtils.getDiscountPrice("¥"+mFreeGoodsBuyInfo.getZk_final_price()+"原价"));
            TextView couponDate = view.findViewById(R.id.coupon_date);
            couponDate.setText(mFreeGoodsBuyInfo.getEnd_time()+"失效");
        }
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dismiss:
                dismiss();
                break;
            case R.id.use:
                if (mOnViewClickListener!=null){
                    mOnViewClickListener.onClick(v);
                }
                break;
        }
    }

    public void setOnViewClickListener(OnViewClickListener onViewClickListener) {
        mOnViewClickListener = onViewClickListener;
    }

    public void setFreeGoodsBuyInfo(FreeGoodsBuyInfo freeGoodsBuyInfo) {
        mFreeGoodsBuyInfo = freeGoodsBuyInfo;
    }
}
